package com.sgl.myview.view;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by XH on 2016/12/2.
 */
public class VDHLayout extends LinearLayout {

    private ViewDragHelper mDragger;

    private View mDragView;
    private View mAutoBackView;
    private View mEdgeTrackerView;

    private Point mAutoBackOriginPos = new Point();
    private Point mMovePos = new Point();

    public VDHLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mDragger = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback()
        {
            @Override
            public boolean tryCaptureView(View child, int pointerId)
            {
                //mEdgeTrackerView禁止直接移动
                return child == mDragView || child == mAutoBackView;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                //Log.e("qwer","    left : =======================>" + left);
                //Log.e("qwer","    dx   : =======================>" + dx);
                if(left > 0){
                    return 0;
                }else if(left < mMovePos.x){
                    return mMovePos.x;
                }else {
                    return left;
                }
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                //Log.e("qwer","    top : --->" + top);
                //Log.e("qwer","    dy  : --->" + dy);
                return mAutoBackOriginPos.y;
            }


            //手指释放的时候回调
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel)
            {
                //mAutoBackView手指释放时可以自动回去
                if (releasedChild == mAutoBackView)
                {
                    Log.e("qwer","    xvel  : --->" + xvel);
                    Log.e("qwer","    yvel  : --->" + yvel);
                    if(xvel > -150){
                        mDragger.settleCapturedViewAt(mAutoBackOriginPos.x, mAutoBackOriginPos.y);
                    }else {
                        mDragger.settleCapturedViewAt(mMovePos.x, mMovePos.y);
                    }
                    invalidate();
                }
            }

            //在边界拖动时回调
            @Override
            public void onEdgeDragStarted(int edgeFlags, int pointerId)
            {
                mDragger.captureChildView(mEdgeTrackerView, pointerId);
            }
        });
        mDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event)
    {
        return mDragger.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        mDragger.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll()
    {
        if(mDragger.continueSettling(true))
        {
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e("qwer","    mAutoBackView.getLeft() : =======================>" + mAutoBackView.getLeft());
        Log.e("qwer","    mAutoBackView.getTop()   : =======================>" + mAutoBackView.getTop());
        Log.e("qwer","    mAutoBackView.getWidth()   : =======================>" + mAutoBackView.getWidth());
        mAutoBackOriginPos.x = mAutoBackView.getLeft();
        mAutoBackOriginPos.y = mAutoBackView.getTop();
        mMovePos.x = -400;
        mMovePos.y = mAutoBackView.getTop();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mDragView = getChildAt(0);
        mAutoBackView = getChildAt(1);
        mEdgeTrackerView = getChildAt(2);
    }
}
